package com.appbee.gdata.picasa.client.ui;

import javax.inject.Inject;

import com.appbee.core.client.design.PageStyle;
import com.appbee.core.client.templates.CommonTemplates;
import com.appbee.gdata.picasa.client.activity.PicasaPhotoActivity;
import com.appbee.gdata.picasa.client.text.PicasaStrings;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

public class PhotoView extends Composite implements PicasaPhotoActivity.View
{

	private static PhotoViewUiBinder uiBinder = GWT
			.create(PhotoViewUiBinder.class);

	interface PhotoViewUiBinder extends UiBinder<Widget, PhotoView>
	{
	}

	@UiField(provided=true)
	PageStyle style;
	
	@UiField(provided = true)
	IPicasaStyle picasaStyle;

	@UiField
	FlowPanel buttonPanel;
	
	Button prevButton;
	
	Button nextButton;

	@UiField
	Button backButton;
	
	@UiField
	Element image;

	private final CommonTemplates html;

	private PicasaPhotoActivity activity;

	@UiField(provided = true)
	PicasaStrings picasaStrings;

	@Inject
	public PhotoView(	PageStyle style,
						IPicasaStyle picasaStyle,
						PicasaStrings picasaStrings,
						CommonTemplates html)
	{
		this.style = style;
		this.picasaStyle = picasaStyle;
		this.picasaStrings = picasaStrings;
		this.html = html;
		
		initWidget(uiBinder.createAndBindUi(this));
		
		prevButton = new Button("Fyrri");
		prevButton.setTitle("Fyrri");
		prevButton.setStyleName(style.prevButton());
		prevButton.addClickHandler(new ClickHandler()
		{
			
			@Override
			public void onClick(ClickEvent event)
			{
				activity.doPrevius();
			}
		});
		nextButton = new Button("Næsta");
		nextButton.setTitle("Næsta");
		nextButton.setStyleName(style.nextButton());
		nextButton.addClickHandler(new ClickHandler()
		{
			
			@Override
			public void onClick(ClickEvent event)
			{
				activity.doNext();
			}
		});
		buttonPanel.add(prevButton);
		buttonPanel.add(nextButton);

		backButton.addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				activity.doBack();
			}
		});
	}

	@Override
	public void show(final String photoUrl)
	{
		image.setInnerHTML(html.img(picasaStyle.fullView(), photoUrl)
				.asString());
	}

	@Override
	public void setActivity(PicasaPhotoActivity picasaPhotoActivity)
	{
		this.activity = picasaPhotoActivity;
	}

	@Override
	public void setPreviusEnabled(boolean enabled)
	{
		prevButton.setVisible(enabled);
	}

	@Override
	public void setNextEnabled(boolean enabled)
	{
		nextButton.setVisible(enabled);
	}

}
